package com.dynamicallo.ty.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dynamicallo.ty.entity.Action;
import com.dynamicallo.ty.entity.Content;
import com.dynamicallo.ty.service.IContentService;
import com.dynamicallo.ty.service.IInterbaleService;
import com.google.common.collect.Lists;

/**
 * 
 * @author wb
 * @date 2016年1月2日 下午9:00:47
 */
@Scope("prototype")
@Controller
@RequestMapping("/content")
public class ContentController {	
	@Autowired
	private IContentService contentService;
	@Autowired
	private IInterbaleService intertableService;
	//返回到jsp
	/**
	 * 能够从request中获取contentId，则跳转到jsp/showContent.jsp
	 * 没有则跳转到jsp/content/showContent.jsp
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showContent")
	public String toIndex(HttpServletRequest request,Model model){
	    if(StringUtils.isNotBlank(request.getParameter("contentId"))){
	        int contentId = Integer.parseInt(request.getParameter("contentId"));
	        Content content = contentService.getContentById(contentId);
	        model.addAttribute(content);
	        return "showContent";
	    }else{
	        //为了对应IndexController的"redirect:/content/showContent";重定向
	        List<Content> contents = contentService.getAllContent();
	        model.addAttribute("contents", contents);
	        return "content/showContentAll";
	    }
	}
	/**
	 * 返回json化的全部content数据
	 * @return
	 */
	@RequestMapping(value="showContentJson",method=RequestMethod.GET )
	public ResponseEntity<List<Content>> listContent(){
	    List<Content> temp = contentService.getAllContent();
	    return  new ResponseEntity<List<Content>>(temp, HttpStatus.OK);
	}
	/**
	 * 返回json化的部分content数据,依据id获取
	 * @param request
	 * @return
	 */
    @RequestMapping(value="showContentJson",method=RequestMethod.GET ,params="one")
    public ResponseEntity<Content> listContent(@RequestParam("contentId") Integer contentId){
        if(contentId!=null){
            Content content = contentService.getContentById(contentId);
            return  new ResponseEntity<Content>(content, HttpStatus.OK); 
        }else{
            return new ResponseEntity<Content>(HttpStatus.EXPECTATION_FAILED);
        }
    }
    /**
     * 通过contentId查找其对应的所有的action,直接跳转到action页面
     * @param contentId
     * @return
     */
    @RequestMapping(value="getActionsByContentId",method=RequestMethod.GET )
    public String getActionsByContentId(@RequestParam("contentId") Integer contentId){
       if(contentId==null){
           //没有contentId返回到showContent
           return "/error";
       }
       return "forward:/action/getActionsByContentIdWithJSP";
    }
    @RequestMapping(value="toAdd")
    public String toAdd(){
        return "content/addContent";
    }
    /**
     * post添加content,返回到jsp视图
     * @param name
     * @param projectname
     * @param url
     * @param remark
     * @param iswork
     * @return 返回到展示页面
     */
    @RequestMapping(value="addContent",method=RequestMethod.POST)
    public String addContent(@RequestParam(value="name") String name,
                     @RequestParam(value="projectname") String projectname,
                     @RequestParam(value="url",required=false) String url,
                     @RequestParam(value="remark",required=false) String remark,
                     @RequestParam(value="iswork") String iswork
            ){
        Content content = new Content();
        content.setName(name);
        content.setProjectname(projectname);
        if(StringUtils.isNotBlank(url)){
            content.setUrl(url);
        }
        if(StringUtils.isNotBlank(remark)){
            content.setRemark(remark);
        }
        if(StringUtils.isNotBlank(iswork)){
            if(StringUtils.equals(iswork, "true")){
                content.setIswork(true); 
            }else if(StringUtils.equals(iswork, "1")){
                content.setIswork(true); 
            }else{
                content.setIswork(false); 
            }
        }else{
            content.setIswork(true);
        }
        int result = contentService.addContent(content);
        System.out.println("##@##addContent result ：" + result);
        return "/showContent";
    }
    /**
     * post添加content,返回json,url必须包含json字段，例如www.baidu.com?id=1&json
     * @param name
     * @param projectname
     * @param url
     * @param remark
     * @param iswork
     * @return 返回到展示页面
     */
    @RequestMapping(value="addContent",method=RequestMethod.POST,params="json")
    public ResponseEntity<Content> addContentWithJson(@RequestParam(value="name") String name,
                     @RequestParam(value="projectname") String projectname,
                     @RequestParam(value="url",required=false) String url,
                     @RequestParam(value="remark",required=false) String remark,
                     @RequestParam(value="iswork") String iswork
            ){
        Content content = new Content();
        content.setName(name);
        content.setProjectname(projectname);
        if(StringUtils.isNotBlank(url)){
            content.setUrl(url);
        }
        if(StringUtils.isNotBlank(remark)){
            content.setRemark(remark);
        }
        if(StringUtils.isNotBlank(iswork)){
            if(StringUtils.equals(iswork, "true")){
                content.setIswork(true); 
            }else if(StringUtils.equals(iswork, "1")){
                content.setIswork(true); 
            }else{
                content.setIswork(false); 
            }
        }else{
            content.setIswork(true);
        }
        int result = contentService.addContent(content);
        System.out.println("##@##addContent result ：" + result);
        System.out.println("##@##addContent result获取的主键 ：" + content.getId());
        return new ResponseEntity<Content>(content, HttpStatus.OK);
    }
    /**
     * 依据contentId字段，删除对应contentId，返回json,url必须包含json字段，例如www.baidu.com?id=1&json
     * @param contentId
     * @return
     */
    @RequestMapping(value="delContent",method=RequestMethod.POST,params="json")
    public ResponseEntity<String> addContentWithJson(@RequestParam(value="contentId") String contentId){
        if(StringUtils.isNoneBlank(contentId)&&StringUtils.isNumericSpace(contentId)){
            Content content = new Content();
            content.setId(Integer.parseInt(contentId));
            content.setIswork(false);
            contentService.updateContentByIdOrOther(content);
            return new ResponseEntity<String>("result:true", HttpStatus.OK);
        }else{
            return new ResponseEntity<String>(HttpStatus.EXPECTATION_FAILED);
        }
        
    }
    
}
